<?php
Authenticate::requireAccounting();
$db = Db::getInstance();
$db->autocommit(false);
try {
	if(!Invoice::edit($_POST['id'], array('status'=>1))){
		throw new Exception("Edit Invoice id {$_POST['id']} failed");
	}
	
	if($_POST['packageId'] == 16){
		$_POST['month'] = 6;
	}
	
	$date = new DateTime();
	$date->add(new DateInterval('P'.($_POST['month']).'M'));
	$dateTimeout = $date->format('Y-m-d');
	
	if(!Product::add($_POST['type'], $_POST['packageId'], $_POST['info'], $_POST['month'], 1, $dateTimeout, $_POST['price'], $_POST['accountId'])){
		throw new Exception('Product::add failed');
	}
	$id = Product::$addId;
	if($_POST['type']=='colo'){
		if(!ProductColo::add($id, 0, 0, 0)){
			throw new Exception('ProductColo::add failed');
		}
	}
	else if($_POST['type']=='vps'){
		if(!ProductVPS::add($id, $_POST['os'], '')){
			throw new Exception('ProductVPS::add failed');
		}
	}
	else if($_POST['type']=='hosting'){
		if(!ProductHosting::add($id, '', $_POST['domain'])){
			throw new Exception('ProductHosting::add failed');
		}
	}
	
	$db->commit();
	echo json_encode(array('success'=> true));
}
catch (Exception $ex) {
	$db->rollback();
	echo json_encode(array('success'=> false));
	throw $ex;
}